//
//  FlutterWxResponseHandler.m
//  Pods-Runner
//
//  Created by lu on 9/11/2019.
//

#import "FlutterWxResponseHandler.h"
#import "AppUtils.h"

@implementation FlutterWxResponseHandler

+ (instancetype)defaultManager {
    static dispatch_once_t onceToken;
    static FlutterWxResponseHandler *instance;
    dispatch_once(&onceToken, ^{
        instance = [[FlutterWxResponseHandler alloc] init];
    });
    return instance;
}

FlutterMethodChannel *fluwxMethodChannel = nil;

- (void)setMethodChannel:(FlutterMethodChannel *)flutterMethodChannel {
    fluwxMethodChannel = flutterMethodChannel;
}

// MARK: - WXApiDelegate

- (void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        if (_delegate
                    && [_delegate respondsToSelector:@selector(managerDidRecvMessageResponse:)]) {
                SendMessageToWXResp *messageResp = (SendMessageToWXResp *) resp;
                [_delegate managerDidRecvMessageResponse:messageResp];
            }
            
            SendMessageToWXResp *messageResp = (SendMessageToWXResp *) resp;

            NSDictionary *result = @{
                    @"description": messageResp.description == nil ? @"" : messageResp.description,
                    @"errStr": messageResp.errStr == nil ? @"" : messageResp.errStr,
                    @"errCode": @(messageResp.errCode),
                    @"type": messageResp.type == nil ? @2 : @(messageResp.type),
                    @"country": messageResp.country == nil ? @"" : messageResp.country,
                    @"lang": messageResp.lang == nil ? @"" : messageResp.lang,
                    @"platform": @"iOS"
            };
            
        NSLog(@"分享成功回调=：%@", result);
        
            [fluwxMethodChannel invokeMethod:@"onWxShareResponse" arguments:result];
    } else if ([resp isKindOfClass:[SendAuthResp class]]) {
        if (_delegate
                && [_delegate respondsToSelector:@selector(managerDidRecvAuthResponse:)]) {
            SendAuthResp *authResp = (SendAuthResp *) resp;
            [_delegate managerDidRecvAuthResponse:authResp];
        }

        SendAuthResp *authResp = (SendAuthResp *) resp;
        NSDictionary *result = @{
                @"description": authResp.description == nil ? @"" : authResp.description,
                @"errStr": authResp.errStr == nil ? @"" : authResp.errStr,
                @"errCode": @(authResp.errCode),
                @"type": authResp.type == nil ? @1 : @(authResp.type),
                @"country": authResp.country == nil ? @"" : authResp.country,
                @"lang": authResp.lang == nil ? @"" : authResp.lang,
                @"platform": @"iOS",
                @"code": [AppUtils nilToEmpty:authResp.code],
                @"state": [AppUtils nilToEmpty:authResp.state]
        };
        
        NSLog(@"授权成功回调=：%@", result);
        
        [fluwxMethodChannel invokeMethod:@"onWxAuthResponse" arguments:result];
    } else if ([resp isKindOfClass:[WXLaunchMiniProgramResp class]]) {
        if ([_delegate respondsToSelector:@selector(managerDidRecvLaunchMiniProgram:)]) {
            [_delegate managerDidRecvLaunchMiniProgram:(WXLaunchMiniProgramResp *) resp];
        }

        WXLaunchMiniProgramResp *miniProgramResp = (WXLaunchMiniProgramResp *) resp;


        NSDictionary *commonResult = @{
                @"description": miniProgramResp.description == nil ? @"" : miniProgramResp.description,
                @"errStr": miniProgramResp.errStr == nil ? @"" : miniProgramResp.errStr,
                @"errCode": @(miniProgramResp.errCode),
                @"type": miniProgramResp.type == nil ? @1 : @(miniProgramResp.type),
                @"platform": @"iOS"
        };
        
        NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:commonResult];
        if (miniProgramResp.extMsg != nil) {
            result[@"extMsg"] = miniProgramResp.extMsg;
        }
        
        NSLog(@"拉起小程序成功回调=：%@", result);
        
        [fluwxMethodChannel invokeMethod:@"onWxLaunchMiniProgramResponse" arguments:result];

    } else if ([resp isKindOfClass:[PayResp class]]) {
        if ([_delegate respondsToSelector:@selector(managerDidRecvPaymentResponse)]) {
            [_delegate managerDidRecvPaymentResponse:(PayResp *) resp];
        }

        PayResp *payResp = (PayResp *) resp;

        NSDictionary *result = @{
                @"description": [AppUtils nilToEmpty:payResp.description],
                @"errStr": [AppUtils nilToEmpty:resp.errStr],
                @"errCode": @(payResp.errCode),
                @"type": payResp.type == nil ? @5 : @(payResp.type),
                @"returnKey": payResp.returnKey == nil ? @"" : payResp.returnKey,
                @"platform": @"iOS"
        };
        
        NSLog(@"微信支付成功回调=：%@", result);
        
        [fluwxMethodChannel invokeMethod:@"onWxPayResponse" arguments:result];
    }
}

- (void)onReq:(BaseReq *)req {
    if ([req isKindOfClass:[GetMessageFromWXReq class]]) {
        if (_delegate
                && [_delegate respondsToSelector:@selector(managerDidRecvGetMessageReq:)]) {
            GetMessageFromWXReq *getMessageReq = (GetMessageFromWXReq *) req;
            [_delegate managerDidRecvGetMessageReq:getMessageReq];
        }
    } else if ([req isKindOfClass:[ShowMessageFromWXReq class]]) {
        if (_delegate
                && [_delegate respondsToSelector:@selector(managerDidRecvShowMessageReq:)]) {
            ShowMessageFromWXReq *showMessageReq = (ShowMessageFromWXReq *) req;
            [_delegate managerDidRecvShowMessageReq:showMessageReq];
        }
    } else if ([req isKindOfClass:[LaunchFromWXReq class]]) {
        LaunchFromWXReq *launchReq = (LaunchFromWXReq *) req;
        NSString *extinfo = launchReq.message.messageExt;
        NSLog(@"h5调起app extinfo: %@", extinfo);
        
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0/*延迟执行时间*/ * NSEC_PER_SEC));
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            [fluwxMethodChannel invokeMethod:@"h5WxLaunchApp" arguments:extinfo];
        });
        
        if (_delegate
                && [_delegate respondsToSelector:@selector(managerDidRecvLaunchFromWXReq:)]) {
            LaunchFromWXReq *launchReq = (LaunchFromWXReq *) req;
            [_delegate managerDidRecvLaunchFromWXReq:launchReq];
        }
    }
}

@end
